var config =require('../config.js');
var AV = require('leanengine');
var async = require('async');
var _ = require("underscore");
var log4js = require('log4js');
var logger = log4js.getLogger('normal');
var signDayUserModel = require('../models/signDayUserModel');

module.exports = function (req, res, next) {
    var url = req.originalUrl;

    //只针对签到页面进行拦截
    if(url.indexOf('activity/signday')<0){
        next(); return;
    }
    //获取当前用户
    var _user = req.AV.user;
    //识别当前用户的质量
    //1.用户等级高，消费高，可通过
    if(_user.level_id > 2){
        next();return;
    }

    var UID = _user.id;
    console.log(UID);
    async.parallel({
        f2:function(cb){
            //2.用户漏签过，鼓励补签，可通过 只玩过10次以内的用户也可以过
            signDayUserModel.getUserLostSignDayNumber(UID).then(function(days){
                if(days.delta > 1 || days.all < 10){
                    //有漏签
                    cb(1);
                }else{
                    cb(null);
                }

            })
        },
        f3:function(cb){
            //3.补签超过5次的也可以通过
            signDayUserModel.getUserFillSignDays(UID).then(function(c){
                if(c >= 5){
                    cb(1);
                }else{
                    cb(null);
                }
            })

        },

        f4:function(cb){
            //4.用户100天下单 >= 20单,可通过
            signDayUserModel.getUserOrdersTheLast100Days(UID).then(function(c){
                if(c >= 20)
                    cb(1);
                else
                    cb(null);
            });
        },
        f5:function(cb){
            //5.用户100天下单实际付款 >= 200 块,可通过
            signDayUserModel.getUserOrderAmountTheLast100Days(UID).then(function(c){
                if(c >= 200)
                    cb(1);
                else
                    cb(null);
            });
        },
        ///*
        f6:function(cb){
            //设定时间段进行拦截概率的调整
            var _now = new Date();
            var _hour = _now.getHours();//当前的小时
            console.log(_hour);
            var _probability = [];   //概率 在 0-9之间，数量越少，阻拦的概率越低，数量越多，阻拦的概率越高
            switch(_hour){
                case 0:
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                case 7:
                    _probability = [1];
                    break;
                case 8:
                case 9:
                case 10:
                case 11:
                case 12:
                case 13:
                case 14:
                case 15:
                case 16:
                case 17:
                case 18:
                    _probability = [1];
                    break;
                case 19:
                case 20:
                case 21:
                case 22:
                case 23:
                    _probability = [1];
                    break;
            }

            var _randomNumber = _.random(0,100);
            if(_.contains(_probability,_randomNumber)){
                //随机数没有出现在概率数组中，通过
                cb(1);
            }else{
                cb(null);
            }
        }//*/

    },function(error,result){
        if(error){
            next();
        }else{
            //TODO:此处跳转到需要拦截的用户看到的界面
            //TODO:添加统计代码
            res.render('activity/signError', {
                title: '签到的人太多啦~~'
            })

        }
    });

};